const { game: gameConfig } = require('./../config');

const PILE_CARD = {
    option: {
        // icon: '',
        target: gameConfig.targets.targetGroup,
        tick: 2,
    },
    handler: {
        use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
            let removeCards = [];

            let min = (card.option.value - 1) * 3 + 1;
            let max = card.option.value * 3;
            let hasCardRoles = targetGroups[0].roles.filter(role => role.hasCard({ min, max }));

            if (hasCardRoles.length) {
                let card = hasCardRoles[Math.floor(hasCardRoles.length * Math.random())].removeCard({ min, max });
                console.log(card);
                if (card) {
                    removeCards.push(card);
                }
            }
            
            return {
                cards: removeCards
            }
        },
        desc(card) {
            return [
                '(立即)',
                '随机',
                '<丢弃>',
                `{1名}`,
                '[敌方玩家]',
                `{1张}`,
                `{${(card.option.value - 1) * 3 + 1}点}`,
                '~', 
                `{${card.option.value * 3}点}`,
                '的',
                '[手牌]',
                '。'
            ]
        }
    }
};

const RECYCLE_CARD = {
    option: {
        // icon: '',
        target: gameConfig.targets.ownRole,
        tick: 3,
    },
    handler: {
        use(card, board, ownGroup, ownRole, ownHome, targetGroups, targetRoles, targetHomes) {
            ownHome.grow(card.option.value);
            ownRole.resetCard(card);
            ownRole.addCard();
            return {
                pile: false
            }
        },
        desc(card) {
            return [
                '(立即)',
                '为',
                '[己方堡垒]',
                '增加',
                `{${card.option.value}点}`,
                '[兵力]',
                '，并将此牌重新',
                '<置入>',
                '[自己]',
                '的',
                '[牌堆]',
                '后，再从',
                '[自己]',
                '[牌堆]',
                '中',
                '<摸入>',
                `{1张}`,
                '[牌]',
                '。'
            ]
        }
    }
};

module.exports = {
    type: 'magician',
    name: '魔术师',
    desc: '魔术师善于操作卡牌，往往在不知不觉间就可变化手中的卡牌，甚至他人手中的牌',
    minSize: 1,
    cards: [
        PILE_CARD,
        PILE_CARD,
        PILE_CARD,
        RECYCLE_CARD,
        RECYCLE_CARD,
        RECYCLE_CARD,
    ]
};